package com.xwd.web.student;

import com.xwd.domain.Student;
import com.xwd.service.StudentService;
import com.xwd.service.impl.StudentServiceImpl;
import com.xwd.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getStudetList")
public class GetStudentListServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        String sex = req.getParameter("sex");
//        System.err.println(stuName);
//        System.err.println(stuNo);
//        System.err.println(sex);

        //获取分页信息
        PageUtil pageUtil = new PageUtil();
        String pageIndex = req.getParameter("pageIndex");
        int index=pageIndex==null?1:Integer.parseInt(pageIndex);
        pageUtil.setIndex(index);
        //调用service方法
        StudentService service = new StudentServiceImpl();
        if(sex==null){
            sex="-1";
        }
        List<Student> students = service.getAllStudent(stuName,stuNo,Integer.parseInt(sex),pageUtil.getIndex(),pageUtil.getPageSize());
        pageUtil.setDataList(students);

        int total = service.total(stuName, stuNo, Integer.parseInt(sex));
        pageUtil.setTotal(total);
        //如果total%pageSize==0 pageCount=total/pageSize
        pageUtil.setPageCount(total%pageUtil.getPageSize()==0?total/pageUtil.getPageSize():total/pageUtil.getPageSize()+1);
        //跳转页面

        //存查询条件信息
        req.setAttribute("stuName",stuName);
        req.setAttribute("stuNo",stuNo);
        req.setAttribute("sex",sex);

        req.setAttribute("pageUtil",pageUtil);
        req.getRequestDispatcher("/Educational/student/list.jsp").forward(req,resp);
    }
}
